Skip to content

Conversation

@MeezaanD
Copy link

No description provided.

app.ts Outdated
let searchResultDisplay = false;
let isSearchMode = false;
let searchResultIndexes: number[] = [];
let specialResizeOccurred = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try sticking to two global variables. And if you set a variable no need to give the type aswell

app.ts Outdated
handleSpecialResize();
};
// Global variables
const IMQS: string = "http://localhost:2050";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase please

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore, as for global constant values we are supposed to always use all caps.

app.ts Outdated
Comment on lines 32 to 33
let recordCount: number = await (await fetch(`${IMQS}/recordCount`)).json();
totalRecordCount = recordCount;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let recordCount: number = await (await fetch(`${IMQS}/recordCount`)).json();
totalRecordCount = recordCount;
let recordCount: number = await fetch(`${IMQS}/recordCount`);
totalRecordCount = JSON.parse(recordCount);

Copy link

@DevinFledermaus DevinFledermaus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a space between the functions

app.ts Outdated
Comment on lines 2 to 10
const IMQS = "http://localhost:2050";
let totalRecordCount: number;
let totalPages: number;
let currentValueOfFirstRecord = 1;
let currentFirstRecordIndex = 1;
let currentPage: number;
let recordsPerPage: number;
let searchedIndex: number | null = null;
let resizeTimeout: number;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Way too many global variables, try for max two

app.ts Outdated
async function totalRecords(): Promise<void> {
try {
let recordCountResponse = await fetch(`${IMQS}/recordCount`);
let recordCountText = await recordCountResponse.text();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to await here

app.ts Outdated
async function displayColumns() {
try {
let columnsResponse = await fetch(`${IMQS}/columns`);
let columns = await columnsResponse.json();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to await here

app.ts Outdated
let columnsResponse = await fetch(`${IMQS}/columns`);
let columns = await columnsResponse.json();
let tableHeaderRow = $("#tableHeaderRow");
columns.forEach((columnName: string) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use a for..of loop instead

app.ts Outdated
Comment on lines 49 to 52
let response = await fetch(
`${IMQS}/records?from=${fromRecord}&to=${fromRecord + maximumRecords - 1
}`
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You making this call multiple times, try having it a function of it's own, similar to totalRecords, and that way displayData wont need to be wont need to be an async function

app.ts Outdated
throw (error)
};
};
async function updatePages(isNavigation = false) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wont be an async when displayData is changed

app.ts Outdated
Comment on lines 148 to 149
const recordsText = await response.text();
const records = JSON.parse(recordsText);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const recordsText = await response.text();
const records = JSON.parse(recordsText);
const records = JSON.parse(response.text());

app.ts Outdated
const searchPageIndex = searchedIndex % recordsPerPage;
currentPage = searchPage;
currentFirstRecordIndex = (searchPage - 1) * recordsPerPage;
await displayData(currentFirstRecordIndex, recordsPerPage);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await displayData(currentFirstRecordIndex, recordsPerPage);
displayData(currentFirstRecordIndex, recordsPerPage);

app.ts Outdated
await searchMethod(currentValueOfFirstRecord);
}
let fromRecord = currentFirstRecordIndex;
await displayData(fromRecord, recordsPerPage);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await displayData(fromRecord, recordsPerPage);
displayData(fromRecord, recordsPerPage);

app.ts Outdated
} catch (error) {
throw (error)
};
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No ; after the declaration of functions

Copy link

@anzelIMQS anzelIMQS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review

app.ts Outdated
return recordCount;
}).catch((error) => {
throw error;
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
});

Missing semi-colon

app.ts Outdated
.then((dataText) => JSON.parse(dataText))
.catch((error) => {
throw error;
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
});

Missing semi-colon

app.ts Outdated
Comment on lines 52 to 53
.then((response) => response.text())
.then((dataText) => JSON.parse(dataText))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just say

Suggested change
.then((response) => response.text())
.then((dataText) => JSON.parse(dataText))
.then((response) => response.json())

Instead of first to text and then JSON.parse.
This is applicable to your other fetches as well.
If it is not valid json, the catch will throw your error at least.

app.ts Outdated
}

// Fetch column names and creates them as table headings
function fetchColumns() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a return type. You are returning a promise of something or void?

app.ts Outdated
updateScreen();
}).catch((error) => {
throw error;
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})
});

Missing semi-colon

app.ts Outdated
}

// Search for records by a given value and display them on the page.
async function searchMethod(searchValue: any) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async function searchMethod(searchValue: any) {
async searchMethod(searchValue: number): Promise<fill in your return type> {

Give return types and parameter type.

app.ts Outdated
searchValue = Math.min(searchValue, (await totalRecords()) - 1);
let targetPage = Math.ceil((searchValue + 1) / state.recordsPerPage);
targetPage = Math.min(targetPage, totalPages);
const lastRecordIndex = (await totalRecords()) - 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You really shouldn't have to call totalRecords multiple times in the same function. Call it once and store the value that the promise returns. Handle it.

app.ts Outdated
} else {
$("#nextPageButton").show();
}
}).catch((error) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}).catch((error) => {
})
.catch((error) => {

Put catches on the next line like the suggestion. The same like you did with the thens

app.ts Outdated
}
})

$("#searchForm").submit(async function (e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$("#searchForm").submit(async function (e) {
$("#searchForm").submit(async (e) => {

app.ts Outdated
Comment on lines 240 to 246
await searchMethod(searchValue);
if (state.searchedIndex !== null) {
currentPage = Math.ceil((state.searchedIndex + 1) / state.recordsPerPage);
state.currentFirstRecordIndex = Math.max(state.searchedIndex - state.recordsPerPage + 1, 0);
};
$("#loader").hide();
$("#tableWrapper").show();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await searchMethod(searchValue);
if (state.searchedIndex !== null) {
currentPage = Math.ceil((state.searchedIndex + 1) / state.recordsPerPage);
state.currentFirstRecordIndex = Math.max(state.searchedIndex - state.recordsPerPage + 1, 0);
};
$("#loader").hide();
$("#tableWrapper").show();
searchMethod(searchValue)
.then(() => {
if (state.searchedIndex !== null) {
currentPage = Math.ceil((state.searchedIndex + 1) / state.recordsPerPage);
state.currentFirstRecordIndex = Math.max(state.searchedIndex - state.recordsPerPage + 1, 0);
}
$("#loader").hide();
$("#tableWrapper").show();
});

Handle your promise rather. It feels cleaner than await. Excuse the indentation in the suggestion.

app.ts Outdated
Comment on lines 1 to 11
let totalPages: number;
let currentPage: number;

const state = {
IMQS: "http://localhost:2050",
currentValueOfFirstRecord: 1,
currentFirstRecordIndex: 1,
recordsPerPage: 16,
searchedIndex: null as number | null,
isButtonDisabled: false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global variables are bad, please remove them.

I noticed that previous comments on the PR indicated a number of global variables to aim for. Please ignore that, as it was meant to be a worst case scenario in case the creator of the PR could not figure out how to do it.

I would recommend moving them to a class along with the methods that are depended on them.

app.ts Outdated
isButtonDisabled: false
}

// Fetch the total number of records from the server.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this comment to match the javadoc style. This is unfortunately not currently in our style guide.

app.ts Outdated
// Fetch the total number of records from the server.
function totalRecords(): Promise<number> {
return fetch(`${state.IMQS}/recordCount`)
.then((recordCountResponse) => recordCountResponse.json())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the () around recordCountResponse or specify a type.

Also, you need to check if the response was good, as .catch is not going to catch that for you.

app.ts Outdated
});
}

// Fetch column names and creates them as table headings
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar javadoc style comment.

app.ts Outdated
// Fetch column names and creates them as table headings
function fetchColumns(): Promise<void> {
return fetch(`${state.IMQS}/columns`)
.then((columnsResponse) => columnsResponse.json())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either remove the () around columnsResponse or specify a type.

Also, you need to check the response was good, as .catch is not going to catch that for you.

index.html Outdated
<button id="prevPageButton">Previous Page</button>
<form id="searchForm">
<div class="form-group">
<input type="number" id="searchInput" placeholder="Search ID" min="1" required />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I can't search for the first record? Bit of an odd choice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use tabs as indentation.

style.css Outdated
Comment on lines 16 to 24
/* Colors */
:root {
--primary-color: white;
--secondary-color: #292929;
--tertiary-color: black;
--shadow-color: #e3e3e3;
--selected-color: #0d6efd;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely want to set your variables first in this file. I would recommend moving it up to be the first thing in this file.

style.css Outdated
background: var(--selected-color);
box-shadow: none !important;
border: 1px solid var(--secondary-color);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting got a bit weird here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ensure an empty line between classes.

app.ts Outdated
currentValueOfFirstRecord: number = 0;
/** Index of the first record currently displayed on the page */
currentFirstRecordIndex: number = 0;
/** Current page number (changes dynamically)*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Current page number (changes dynamically)*/
/** Current page number (changes dynamically) */

app.ts Outdated
recordsPerPage: number = 16;
/** Index of the record being searched for (null if not searching) */
searchedIndex: number | null = null;
/** Checks if the button is enabled/disabled*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Checks if the button is enabled/disabled*/
/** Checks if the button is enabled/disabled */

app.ts Outdated
Comment on lines 19 to 38
const loadApp = new Promise<void>((resolve, reject) => {
window.onload = () => {
try {
$("#loader").hide();
$(window).on("resize", this.debounce(() => {
this.updateScreen();
}, 250));
this.fetchColumns();
this.updateScreen();
this.eventHandlers();
resolve();
} catch (error) {
reject(error);
}
};
});
loadApp
.catch(() => {
throw new Error('Error during window.onload:');
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do this somewhere else... a constructor should not be doing anything that can result in an error being thrown. And it should also definitely not be doing any asynchronous calls.

app.ts Outdated
if (!recordCountResponse.ok) {
throw new Error('Error trying to get recordCount');
}
return recordCountResponse.json();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure /recordCount does not return a json... Pretty sure you want to return text() and then parse the text in the next .then()

app.ts Outdated
.then(columnsResponse => {
if (!columnsResponse.ok) {
throw new Error('Error trying to fetch the columns');
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this return, as you should not have a return statement after a throw

app.ts Outdated
if (this.currentPage >= this.totalPages) {
const errorMessage = "Already on the last page";
window.alert(errorMessage);
throw new Error(errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are the last line of defense, you can't throw an error here. But it looks like you are all ready handling the error with window.alert, so I would just replace this line with a return.

app.ts Outdated
$("#prevPageButton").hide();
$("#tableWrapper").hide();
$("#loader").show();
let totalRecCount: number | void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value is not set anywhere... I think you meant to place it in the .then that handles the promise coming from totalRecords.

app.ts Outdated
return this.displayData(fromRecord, this.recordsPerPage);
})
.catch((error) => {
throw error;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are the last line of defense, you need to handle the error here.

app.ts Outdated
}

/** Initialize the app when the page loads */
new InitializeApp();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels out of place without the window.onload = () => { ... }; mentioned in the readme of this project. Not saying your code has to look like that, but I would struggle to have something to comment about if you still used the example that we provided in the readme.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is still relevant.

@MeezaanD MeezaanD self-assigned this Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants